/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.dialogs.PropertyPage;

/**
  * @version $Revision: 146 $
  * @author  <a href="bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PerforceServerReferencesPropertyPage extends PropertyPage
{
	// --------------------------------------------------------------------------- Private Constants
	
	/** Constant: Width Hint for Table */
	private static final int WIDTH_HINT = 300;
	/** Constant: Height Hint for Table */
	private static final int HEIGHT_HINT = 100;

	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the Perforce Server. */
	private IPerforceServer perforceServer;
	
	/** Table Viewer: Referenced Projects */		
	private TableViewer viewer;
	
	// --------------------------------------------------------------------------- Protected Methods
	
	/** @see PreferencePage#createContents(org.eclipse.swt.widgets.Composite) */
	protected Control createContents(Composite parent) 
	{
		initialize();
		noDefaultAndApplyButton();
		
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		
		createReferencingProjectsArea(composite);
		
		return composite;
	}
	
	// ----------------------------------------------------------------------------- Private Methods

	/** Creates an area showing the projects that currently reference the perforce server.
	  *
	  * @param parent the parent composite.
	  */
	private void createReferencingProjectsArea(Composite parent) 
	{
		int style = SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL;

		Table table = new Table(parent, style);		
		
		TableLayout layout = new TableLayout();
		layout.addColumnData(new ColumnWeightData(1));
		
		table.setLayout(layout);
		table.setLinesVisible(true);
		
		viewer = new TableViewer(table);
		
		GridData data = new GridData(GridData.FILL_BOTH);
		data.widthHint = WIDTH_HINT;
		data.heightHint = HEIGHT_HINT;

		table.setLayoutData(data);
				
		try
		{
			IProject[] projects = 
				PerforceCorePlugin.getServerManager().getReferencingProjects(perforceServer);
			for (int i = 0; i < projects.length; i++)
			{
				viewer.add(projects[i].getName());
			}
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus());
		}	
	}

	/** Called to initialize the property page. */
	private void initialize() 
	{
		IAdaptable element = getElement();
		if (element instanceof IPerforceServer) 
		{
			perforceServer = (IPerforceServer) element;
		} 
		else 
		{
			Object adapter = element.getAdapter(IPerforceServer.class);
			if (adapter instanceof IPerforceServer) 
			{
				perforceServer = (IPerforceServer) adapter;
			}
		}
	}	

}
